package com.xsky.web
{
	import mx.rpc.AbstractOperation;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.soap.WebService;
	
	public class WebSrv
	{
		private var webService:WebService = new WebService();
		public var onComplete:Function = null;
		public var onError:Function = null;
		public function WebSrv(strWsdl:String)
		{
			webService.loadWSDL(strWsdl);
			webService.addEventListener("result", resultHandel);
			webService.addEventListener("fault", faultHandel);	
		}
		public function invokeWebSrv(serviceName:String, args:Object):void
		{
			var operation:AbstractOperation = webService[serviceName];
			operation.arguments = args;
			operation.send();
		}
		public function resultHandel(event:ResultEvent):void
		{
			if(onComplete!=null)
			{
				if(event.result!=null)
				{
					onComplete(event.result.toString());
				}
			}
		}
		public function faultHandel(event:FaultEvent):void
		{
			if(onError!=null)
			{
				onError(event.message);
			}
		}
		public function clear():void
		{
			webService.removeEventListener(FaultEvent.FAULT, faultHandel);
			webService.removeEventListener(ResultEvent.RESULT,resultHandel);
			webService.disconnect();
			webService = null;
		}
	}
}